﻿<template>
  <el-container style="height: 800px; border: 0px solid #eee">
    <el-aside width="300px">
      <el-menu
        default-active="2"
        class="el-menu-vertical-demo"
        @open="handleOpen"
        @close="handleClose"
      >
        <router-link to="/page">
          <el-menu-item index="1"><span>表白墙</span></el-menu-item>
        </router-link>
        <router-link to="/market">
          <el-menu-item index="2"><span>跳蚤市场</span></el-menu-item>
        </router-link>
        <router-link to="/recruit">
          <el-menu-item index="3"><span>招募</span></el-menu-item>
        </router-link>
        <router-link to="/find">
          <el-menu-item index="4"><span>找搭子</span></el-menu-item>
        </router-link>
        <router-link to="/article">
          <el-menu-item index="5"><span>发帖</span></el-menu-item>
        </router-link>
        <router-link to="/person">
          <el-menu-item index="6"><span>个人中心</span></el-menu-item>
        </router-link>
      </el-menu>
    </el-aside>

    <el-main>
      <h1>帖子列表</h1>
      <el-row :gutter="6">
        <el-col
          v-for="post in posts"
          :key="post.id"
          :span="7"
          style="margin-bottom: 20px"
        >
          <el-card shadow="hover" @click.native="openDialog(post)">
            <h2>{{ post.title }}</h2>
            <p class="ellipsis">{{ post.content }}</p>
            <p style="text-align: left">用户:{{ post.username }}</p>
            <p style="text-align: left">
              发布时间:{{
                post.createTime
                  .toLocaleString()
                  .replace(/T/g, " ")
                  .replace(/\.[\d]{3}Z/, "")
              }}
            </p>
          </el-card>
        </el-col>
      </el-row>
    </el-main>

    <el-dialog :visible.sync="dialogVisible" title="帖子详情">
      <!-- 这里放置帖子详情内容 -->
      <h2 v-if="selectedPost">{{ selectedPost.title }}</h2>
      <p v-if="selectedPost">{{ selectedPost.content }}</p>
      <br />
      <p style="text-align: left" v-if="selectedPost">
        用户:{{ selectedPost.username }}
      </p>
      <p style="text-align: left" v-if="selectedPost">
        发布时间:{{
          selectedPost.createTime
            .toLocaleString()
            .replace(/T/g, " ")
            .replace(/\.[\d]{3}Z/, "")
        }}
      </p>
    </el-dialog>
  </el-container>
</template>

<style>
a {
  text-decoration: none;
}

.card-content {
  max-height: 100px; /* 设置最大高度 */
  overflow: hidden; /* 隐藏超出的内容 */
}

.ellipsis {
  text-overflow: ellipsis; /* 文本溢出时显示省略号 */
  white-space: nowrap; /* 防止文本换行 */
  overflow: hidden; /* 隐藏溢出的文本 */
}
</style>

<script>
import axios from "axios";

export default {
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    // 在点击Card时打开Dialog
    openDialog(post) {
      this.selectedPost = post;
      this.dialogVisible = true;
    },
  },
  data() {
    return {
      posts: [],
      dialogVisible: false, // 在data选项中初始化dialogVisible
      selectedPost: null,
    };
  },
  created() {
    // 在组件创建时获取帖子数据
    axios.get("http://localhost:8082/posts/market").then((response) => {
      this.posts = response.data;
    });
  },
};
</script>